<%--
  Created by IntelliJ IDEA.
  User: 70481
  Date: 2019/9/10
  Time: 16:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">持明法州后台管理</div>
        <!-- 头部区域（可配合layui已有的水平导航） -->
        <ul class="layui-nav layui-layout-left">
            <li class="layui-nav-item"><a href="">控制台</a></li>
            <li class="layui-nav-item"><a href="">商品管理</a></li>
            <li class="layui-nav-item"><a href="">用户</a></li>
            <li class="layui-nav-item">
                <a href="javascript:;">其它系统</a>
                <dl class="layui-nav-child">
                    <dd><a href="">邮件管理</a></dd>
                    <dd><a href="">消息管理</a></dd>
                    <dd><a href="">授权管理</a></dd>
                </dl>
            </li>
        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:;">
                    <img src="http://t.cn/RCzsdCq" class="layui-nav-img">
                    <shiro:principal/>
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">基本资料</a></dd>
                    <dd><a href="">安全设置</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item"><a href="">退了</a></li>
        </ul>
    </div>

    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->

            <ul class="layui-nav layui-nav-tree"  lay-filter="test" id="menu">

            </ul>
        </div>
    </div>

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">

            <div class="layui-tab layui-tab-brief" lay-allowClose="true" lay-filter="myTab">
                <ul class="layui-tab-title">
                    <li class="layui-this">欢迎</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">欢迎进入持明法州后台管理</div>
                </div>
            </div>

        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script src="${pageContext.request.contextPath}/js/jquery-3.4.1.js"></script>
<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use('element', function(){
        var element = layui.element;

    });

    $(function(){
        // 发送ajax请求
        $.ajax({
            url:"${pageContext.request.contextPath}/menu/queryMenu",
            success:function (data) {
                console.log(data)
                var html = "";
               $.each(data,function (maxIndex,maxMenu) {
                   var maxStart = " <li class=\"layui-nav-item layui-nav-itemed\">\n" +
                       "                    <a class=\"\" href=\"javascript:void(0);\">";
                   var maxEnd = "</a>";
                   // 拼接一级菜单
                   html+=maxStart+maxMenu.menuName+maxEnd;

                   $.each(maxMenu.menuList,function(minIndex,minMenu){
                       var minStart="<dl class=\"layui-nav-child\">\n" +
                           "                        <dd><a href=\"javascript:void(0);\" onclick='addIfream(\""+minMenu.menuName+"\",\""+minMenu.menuUrl+"\","+minMenu.menuId+
                           ")'>";
                       var minEnd="</a></dd></dl>";
                       // 拼接二级菜单
                        html+=minStart+minMenu.menuName+minEnd;
                   })
                   html+=maxEnd;

               })

                $("#menu").html(html);

               // 重新渲染
                layui.use('element', function(){
                    var element = layui.element;

                    element.render("nav","test")
                });

            }
        })

    })
    function addIfream(menuName,menuUrl,menuId) {
        // console.log(menuName,menuUrl,menuId)

        // 引入标签
        layui.use('element', function(){
            var element = layui.element;
            // 添加一个对象的选项卡
            if($("[lay-id='"+menuId+"']").length===0){
                element.tabAdd("myTab",{
                    title:menuName,
                    content:"<iframe id='triageIframe' src='${pageContext.request.contextPath}"+menuUrl+"?id="+Math.random()+"' width='100%' height='100%'></iframe>",
                    id:menuId
                })
            }
            $("#triageIframe").prop("src","${pageContext.request.contextPath}"+menuUrl+"?id="+Math.random()+"")

            element.tabChange('myTab', menuId)
        });

    }


</script>
</body>
</html>